package com.blockchain.front.servlets;

import java.io.IOException;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.blockchain.common.enums.CertificateType;
import com.blockchain.framework.resource.PromptLevel;
import com.blockchain.framework.service.ServiceSession;
import com.blockchain.framework.service.exception.LogicalException;
import com.blockchain.framework.service.exception.ParameterException;
import com.blockchain.front.service.common.UserManage;
import com.blockchain.util.parser.EnumParser;

public class RegisterThree extends AbstractFrontServlet {
	private static final long serialVersionUID = 1L;

	@Override
	protected void processPost(final HttpServletRequest request, HttpServletResponse response,
			ServiceSession serviceSession) throws Throwable {
		String idcard = request.getParameter("zjh");
		String name = request.getParameter("name");
		CertificateType certificateType = EnumParser.parse(CertificateType.class, request.getParameter("idType"));
		UserManage userManage = serviceSession.getService(UserManage.class);
		userManage.addSmrz(name, idcard, certificateType);
		sendRedirect(request, response, getController().getViewURI(request, RegisterFour.class));
	}

	@Override
	protected void onThrowable(HttpServletRequest request, HttpServletResponse response, Throwable throwable)
			throws ServletException, IOException {
		getResourceProvider().log(throwable.getMessage());
		if (throwable instanceof LogicalException || throwable instanceof ParameterException) {
			prompt(request, response, PromptLevel.ERROR, throwable.getMessage());
			forwardView(request, response, getClass());
		} else if (throwable instanceof SQLException) {
			prompt(request, response, PromptLevel.ERROR, "系统繁忙，请稍后再试");
			forwardView(request, response, getClass());
		} else {
			super.onThrowable(request, response, throwable);
		}
	}
}
